home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / doc.lha / doc.doc / toolbox.doc < prev    next >
Text File  |  1992-09-25  |  39KB  |  1,121 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ___________________________________________________________________
  12.  
  13.  
  14.                                    A Tool Box for
  15.                                    Compiler Construction
  16.  
  17.                                    J. Grosch
  18.                                    H. Emmelmann
  19.  
  20. ___________________________________________________________________
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. ___________________________________________________________________
  51.                                    GESELLSCHAFT FUeR MATHEMATIK
  52.                                    UND DATENVERARBEITUNG MBH
  53.  
  54.                                    FORSCHUNGSSTELLE FUeR
  55.                                    PROGRAMMSTRUKTUREN
  56.                                    AN DER UNIVERSITAeT KARLSRUHE
  57. ___________________________________________________________________
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.                                    Project
  76.  
  77.                              Compiler Generation
  78.  
  79.          ___________________________________________________________
  80.  
  81.                      A Tool Box for Compiler Construction
  82.  
  83.  
  84.                                  Josef Grosch
  85.                                Helmut Emmelmann
  86.  
  87.  
  88.                                 Jan. 21, 1990
  89.  
  90.          ___________________________________________________________
  91.  
  92.  
  93.                                 Report No. 20
  94.  
  95.  
  96.                              Copyright c 1990 GMD
  97.  
  98.  
  99.             Gesellschaft fuer Mathematik und Datenverarbeitung mbH
  100.                 Forschungsstelle an der Universitaet Karlsruhe
  101.                           Vincenz-Priesznitz-Str. 1
  102.                                D-7500 Karlsruhe
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                                                              1
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.                      A Tool Box for Compiler Construction
  142.  
  143.  
  144.                            J. Grosch, H. Emmelmann
  145.               GMD Forschungsstelle an der Universitaet Karlsruhe
  146.              Vincenz-Priesznitz-Str. 1, D-7500 Karlsruhe, Germany
  147.            E-Mail: grosch@karlsruhe.gmd.de, emmel@karlsruhe.gmd.de
  148.  
  149.  
  150.  
  151.  
  152. Abstract
  153.  
  154.      This paper presents a set of tools supporting the construction of  nearly
  155. every  compiler phase.  Design goals of this tool box have been practical usa-
  156. bility, significantly reduced effort for compiler construction, and high qual-
  157. ity  of  the generated compilers.  Especially efficiency should be competitive
  158. to hand crafting.
  159.  
  160.      Currently modules in the target languages C  and  Modula-2  can  be  gen-
  161. erated.  First realistic applications demonstrate the excellent performance of
  162. the tools and show that the tools allow the construction of production quality
  163. compilers.
  164.  
  165. 1.  Introduction
  166.  
  167.      Within this paper we present a compiler generation tool box.  It contains
  168. tools for nearly every compiler phase. We believe the tools are applicable for
  169. realistic compiler projects.
  170.  
  171.      The tools generally accept as input a specification written in a language
  172. specific to the tool and produce modules in a target language (C or Modula-2).
  173. Therefore a tool can be seen as a generic solution of a  compilation  subprob-
  174. lem, which is instantiated by the specification.
  175.  
  176.      Using tools instead of hand crafting a compiler has  several  advantages:
  177. The  effort necessary to build a compiler is substantially reduced. Instead of
  178. writing a program a much shorter specification has to be developed. The  tools
  179. can  perform  many consistency checks on the specifications. Writing automati-
  180. cally checked specifications drastically reduces the amount of possible errors
  181. and hence increases the reliability of the resulting compiler.
  182.  
  183.      The tool box consists of the following tools:
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                                                                              2
  200.  
  201.  
  202.     Rex     scanner generator
  203.     Lalr    LALR(1) parser generator
  204.     Ell     LL(1) parser generator
  205.     Ast     generator for abstract syntax trees
  206.     Ag      attribute evaluator generator
  207.     Estra   transformation of attributed syntax trees
  208.     Beg     back end generator
  209.     Reuse   library of reusable modules
  210.  
  211.  
  212.      All the tools were originally programmed in Modula-2 and run under  UNIX.
  213. Using  the  Modula-2 to C translator called Mtc [Mar90] (see section 6.1), the
  214. sources also exist in C. Currently most of the tools generate modules  in  the
  215. target languages C and Modula-2.
  216.  
  217.      The next two sections present the design goals and the common features of
  218. the  tools.   Section  4 describes the compiler model we prefer.  In section 5
  219. all the tools are sketched briefly.  Section 6 reports about  the  experiences
  220. of  two  realistic  applications  of the tools.  Section 7 gives a summary and
  221. describes future work.
  222.  
  223. 2.  Design Goals
  224.  
  225.      The major design goals of the tool box were:
  226.  
  227. -    practical usability for realistic languages
  228.  
  229. -    automatic generation of production quality compilers
  230.  
  231. -    substantial increase in compiler construction productivity  and  compiler
  232.      reliability
  233.  
  234. -    quality of the resulting compiler competitive to hand crafting
  235.  
  236.      By defining these goals we wanted to produce a tool  box  which  will  be
  237. used  in  practical  compiler construction work.  Therefore we considered com-
  238. petitiveness to hand crafting important, because we feel that tools  promising
  239. a  high  productivity  and  reliability but which produce compilers whose code
  240. quality or efficiency is lower than hand crafted  compilers,  will  hardly  be
  241. used.
  242.  
  243. 3.  Common Implementation Decisions
  244.  
  245.      Our design goals lead to several design decisions common to  all  of  our
  246. tools.  Nearly  every  tool needs a programming language in which the user can
  247. specify certain actions, conditions, or calculations. That is  obviously  true
  248. for  attribute  grammars,  but  also  the back end generator needs to evaluate
  249. several attributes and conditions. Even the  parser  generators  need  such  a
  250. language for the specification of semantic actions.
  251.  
  252.      We decided to select the target language (namely C or Modula-2).  Specif-
  253. ications  therefore  may contain pieces of target language code.  Besides some
  254. pattern replacement the text is copied unchanged  to  the  generated  modules.
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                                                                              3
  266.  
  267.  
  268. The  disadvantage  of  that method is that the target language code can not be
  269. checked completely by the tool. For example the attribute grammar tool can not
  270. check if attribute evaluations do not have side-effects.  On the other hand it
  271. gives a great deal of flexibility, as the whole power of the  target  language
  272. is  available.   It  also  drastically  increases  the practical usability, as
  273. interfacing to other components (perhaps hand-written ones) is  easily  possi-
  274. ble.   It  finally keeps the tools and the specification languages simple. The
  275. user is not forced to learn a new language to express conditions or actions.
  276.  
  277.      Our experience with prior tools is that during realistic compiler  design
  278. a  lot  of small special problems occur, which the tool can not handle. There-
  279. fore loopholes, possibilities how the user of the tools  can  easily  plug  in
  280. hand-written  parts, are necessary. Loopholes also allow to keep tools simple,
  281. as one is not forced to provide a solution for  every  special  case,  immedi-
  282. ately.  It  is  possible  to  use the loophole until a really good solution is
  283. found to be build in a tool.
  284.  
  285.      The tools are largely independent of each other. This is achieved by  the
  286. property  that  none  of  the  generated  modules  has a fixed kind of output.
  287. Instead this output is specified using statements from the target language and
  288. thus  can  be  chosen arbitrarily.  The independence of the tools allows for a
  289. large degree of freedom in the compiler design. An exception are the tools  Ag
  290. and  Estra  which operate on syntax trees specified using Ast.  Therefore they
  291. depend on Ast and all three tools require the compiler to  use  an  attributed
  292. abstract syntax tree.
  293.  
  294. 4.  Compiler Model
  295.  
  296.      Although the tools do not directly enforce any specific  compiler  model,
  297. we  want to present the model we prefer and which we believe is supported best
  298. by the tools. We still consider semantic analysis to be the  hard  part  of  a
  299. compiler.  Therefore we base semantic analysis and the generation of an inter-
  300. mediate language on the abstract syntax. We explicitly construct the  abstract
  301. syntax tree which might be decorated with attributes during semantic analysis.
  302. Besides the abstract syntax, which can be regarded  as  a  first  (high-level)
  303. intermediate  representation, we prefer to use a second (low-level) intermedi-
  304. ate representation as interface to the code generator. This has advantages for
  305. optimizations and for pattern directed code selection.
  306.  
  307.      Figure 1 shows our preferred compiler model. In the right column are  the
  308. main  modules  that constitute a compiler. The left column contains the neces-
  309. sary specifications. In between there are the tools which  are  controlled  by
  310. the  specifications  and  which  produce the modules. The arrows represent the
  311. data flow in part during generation time and in part during run time.
  312.  
  313.      In principle the compiler model works as follows: a scanner and a  parser
  314. read  the  source, check the concrete syntax, and construct an abstract syntax
  315. tree. They may perform several normalizations, simplifications, or transforma-
  316. tions  in  order  to  keep  the  abstract  syntax  relatively simple. Semantic
  317.  
  318.                             Fig. 1: Compiler Model
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.                                                                              4
  331.  
  332.  
  333. analysis is performed on the abstract syntax tree. Optionally  attributes  for
  334. code  generation  may  be  computed.  Afterwards  the  abstract syntax tree is
  335. transformed into an intermediate representation. The latter is  the  input  of
  336. the code generator which finally produces the machine code.
  337.  
  338. 5.  The Tools
  339.  
  340.      The following sections briefly sketch the tools that  make  up  the  tool
  341. box. We only describe the features of the tools - for details, for the specif-
  342. ication techniques, or for examples there exist separate documents.
  343.  
  344. 5.1.  Rex
  345.  
  346.      The scanner generator Rex has been developed with the aim to combine  the
  347. powerful  specification  method  of regular expressions with the generation of
  348. highly efficient scanners [Gro87b, Gro88, Gro89a].  The name  Rex  stands  for
  349. regular expression tool, reflecting the specification method.
  350.  
  351.      A scanner specification consists in principle of a set of regular expres-
  352. sions  each  associated  with a semantic action. Whenever a string constructed
  353. according to a regular expression is recognized in the input  of  the  scanner
  354. its semantic action which is a sequence of arbitrary statements written in the
  355. target language is executed. To be able to recognize tokens depending on their
  356. context,  Rex  provides start states to handle left context and the right con-
  357. text can be specified by an additional regular expression.  If several regular
  358. expressions  match  the  input  characters, the longest match is preferred. If
  359. there are still several possibilities, the regular expression given  first  in
  360. the specification is chosen.
  361.  
  362.      Rex generated scanners automatically provide the line and column position
  363. of  each token. For languages like Pascal and Ada where the case of letters is
  364. insignificant tokens can be normalized to  lower  or  upper  case.  There  are
  365. predefined  rules to skip white space like blanks, tabs, or newlines and there
  366. is a mechanism to handle include files.  The  generated  scanners  are  table-
  367. driven  deterministic  finite  automatons. The tables are compressed using the
  368. so-called comb-vector technique [ASU86].
  369.  
  370.      The most outstanding feature of Rex is its speed. The generated  scanners
  371. process  nearly 200,000 lines per minute without hashing of identifiers and up
  372. to 150,000 lines per minute if hashing is applied.  (Keywords do  not  require
  373. hashing if they are recognized directly by the generated automaton.) This is 4
  374. times the speed of Lex [Les75] generated scanners. In typical cases  Rex  gen-
  375. erated scanners are 4 times smaller then Lex generated ones. Usually Rex takes
  376. only 1/10 of the time of Lex to generate a scanner.
  377.  
  378. 5.2.  Lalr
  379.  
  380.      The parser generator Lalr has been developed with the aim  to  combine  a
  381. powerful  specification  technique for context-free languages with the genera-
  382. tion of highly efficient parsers [Gro88, GrV88].  As it processes the class of
  383. LALR(1) grammars we chose the name Lalr to express the power of the specifica-
  384. tion technique.
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.                                                                              5
  396.  
  397.  
  398.      The grammars may be written using EBNF constructs. Each grammar rule  may
  399. be  associated with a semantic action consisting of arbitrary statements writ-
  400. ten in the target language. Whenever a grammar rule is recognized by the  gen-
  401. erated  parser  the associated semantic action is executed. A mechanism for S-
  402. attribution (only synthesized attributes) is provided to  allow  communication
  403. between the semantic actions.
  404.  
  405.      In case of LR-conflicts a derivation tree is printed to ease the location
  406. of  the  problem.  The  conflict  can be resolved by specifying precedence and
  407. associativity for terminals and rules.  Syntactic  errors  are  handled  fully
  408. automatically  by  the  generated parsers including error reporting, recovery,
  409. and repair.  The generated parsers are table-driven. Like in the case of  Rex,
  410. comb-vector  technique  is  used  to compress the parse tables.  The generator
  411. uses the algorithm described by [DeP82] to compute the look-ahead sets.
  412.  
  413.      Parsers generated by Lalr are two to three times faster than Yacc [Joh75]
  414. generated  ones.  They reach a speed of 580,000 lines per minute on a MC 68020
  415. processor excluding the time for scanning. The size of  the  parsers  is  only
  416. slightly increased in comparison to Yacc, because there is a small price to be
  417. paid for the speed.
  418.  
  419. 5.3.  Ell
  420.  
  421.      The parser generator Ell processes LL(1) grammars which may contain  EBNF
  422. constructs  and  semantic  actions.  It  generates  recursive  descent parsers
  423. [Gro88, GrV88, Gro89b].  A mechanism for  L-attribution  (inherited  and  syn-
  424. thesized attributes evaluable during one preorder traversal) is provided. Like
  425. Lalr, syntax errors are handled fully automatic including error reporting from
  426. a  prototype  error module, error recovery, and error repair.  Those satisfied
  427. with the restricted power of LL(1) grammars may profit from the high speed  of
  428. the generated parsers which lies around 900,000 lines per minute.
  429.  
  430. 5.4.  Ast
  431.  
  432.      Ast is a generator for abstract syntax trees [Gro91a, Gro91b].   It  gen-
  433. erates  program  modules  or  abstract  data types to handle attributed trees.
  434. Besides trees attributed graphs can be handled as well.  The  nodes  of  these
  435. data  structures may be associated with arbitrary many attributes of arbitrary
  436. type.  The specifications for this tool are based on extended  tree  grammars.
  437. They  can be regarded as a common notation for concrete and abstract syntax as
  438. well as for attributed trees and graphs. An extension mechanism provides  sin-
  439. gle  inheritance.  Internally the trees are stored as linked records.  Ast can
  440. be requested to generate many operations for trees: node constructors  combine
  441. aggregate  notation  and  storage  management.  Reader  and  writer procedures
  442. transfer graphs from/to files in ASCII or binary format. The order of subtrees
  443. in  a  list can be reversed. Procedures for commonly used traversal strategies
  444. like top down (depth first) or bottom up (reverse depth first)  are  provided.
  445. An interactive graph browser allows the inspection of graphs in a readable way
  446. and thus supports debugging.
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.                                                                              6
  461.  
  462.  
  463. 5.5.  Ag
  464.  
  465.      Ag is an attribute evaluator generator  [Gro89d,  Gro90].   It  processes
  466. ordered  attribute  grammars  (OAGs) [Kas80] as well as higher order attribute
  467. grammars (HAGs) [VSK89].  It is based on the abstract syntax  or  to  be  more
  468. specific  on  tree  modules generated by Ast.  Therefore the tree structure is
  469. fully known.  The terminals and nonterminals may have  arbitrary  many  attri-
  470. butes  which  can  have  any  target language type.  This includes tree-valued
  471. attributes.  Ag allows attributes local  to  rules  and  offers  an  extension
  472. mechanism which provides (single) inheritance for attributes and for attribute
  473. computations.  It also allows the elimination of chain rules.   The  attribute
  474. computations  are  expressed in the target language and should be written in a
  475. functional style.  It is possible to call  external  functions  of  separately
  476. compiled modules.  Non-functional statements and side-effects are possible but
  477. require careful consideration.  The syntax of the  specification  language  is
  478. designed  to  support  compact, modular, and readable documents.  An attribute
  479. grammar can consist of several  modules  where  the  context-free  grammar  is
  480. specified  only  once.   There  are  shorthand  notations  for  copy rules and
  481. threaded attributes which allow the user to omit many trivial attribute compu-
  482. tations.   The  generated  evaluators  are  very  efficient  because  they are
  483. directly coded using recursive procedures.  Attribute storage is optimized  by
  484. implementing  attributes  as local variables and procedure parameters whenever
  485. their lifetime is contained within one visit.
  486.  
  487. 5.6.  Estra
  488.  
  489.      Estra is a generator for efficient syntax tree transformers [Vie89].  The
  490. generated  transformation  modules take an attributed tree as input and map it
  491. to an arbitrary output.  The output can be a new tree, a linear code  such  as
  492. e.  g.  P-Code,  source  code  like  e. g.  Pascal, or a sequence of procedure
  493. calls. In any case the input tree remains  unchanged  in  order  to  avoid  to
  494. reevaluate the attributes for reasons of consistency. However, subtrees of the
  495. input tree may be reused to construct an output tree. The intended application
  496. areas  for  the transformations are the generation of intermediate representa-
  497. tions out of abstract syntax trees and optimizers operating on  internal  tree
  498. representations  of any level.  Estra cooperates with Ast: the input trees are
  499. constructed by modules generated with the latter tool.
  500.  
  501.      The specification of a transformer is rule based. A rule  consists  of  a
  502. pattern describing a tree fragment and an action. Actions are composed of tar-
  503. get language statements. It is possible to  specify  several  transformations.
  504. The  subtrees  of  a  pattern  can  be  transformed  in any order. They can be
  505. transformed several times by the same or by  different  transformations.   The
  506. actions  have read access to the attributes of the input tree. Additional syn-
  507. thesized and inherited attributes may be evaluated during the  transformation.
  508. The  application  of rules can be restricted by conditions. Ambiguities may be
  509. resolved using costs.
  510.  
  511.      Two implementations of the pattern-matcher can be  selected:  a  directly
  512. coded dynamic programming algorithm or a table-driven tree pattern-matcher. In
  513. both cases the transformation has two phases. While the first  one  determines
  514. the patterns that match with minimal costs the second one executes the associ-
  515. ated actions.
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.                                                                              7
  527.  
  528.  
  529. 5.7.  Beg
  530.  
  531.      Beg (a back end generator) produces code selectors and  register  alloca-
  532. tors  [Emm89a, Emm89b].  Code selection is performed using tree pattern match-
  533. ing.  The target instructions are described using rules containing  tree  pat-
  534. terns.   The  resulting  code  generator  accepts a tree oriented intermediate
  535. language. An input tree is translated by covering the tree by the patterns and
  536. afterwards  emitting the corresponding instructions.  Rules are annotated with
  537. cost values which allows the code generator to select a cover of minimal cost,
  538. that means the sum of the costs of all rules in the cover is minimal.
  539.  
  540.      Therefore the user only describes ambiguously  how  certain  intermediate
  541. language constructs can be translated. He need not to program the algorithm to
  542. select the best way to translate a specific input tree. A good way to  develop
  543. a  code  generator  description  is  to  first  describe  only a subset of the
  544. machine's instructions, big enough to compile the whole language. This results
  545. in  a running compiler, which may produce inefficient code.  Afterwards gradu-
  546. ally more and more rules can be added which finally leads to a  compiler  pro-
  547. ducing good code.
  548.  
  549.      Beg implements the determination of the minimal cover  using  a  directly
  550. coded version of the dynamic programming algorithm [Emm89a, ESL89].
  551.  
  552.      The generation of register allocators is of specific importance,  because
  553. hand  crafting is a rather difficult and tedious job and because errors in the
  554. register allocator are sometimes very difficult to find.   Within  rules,  the
  555. characteristics  with  respect to register allocation of an instruction can be
  556. specified: the allowed registers for each operand, the  registers  changed  by
  557. side-effects, and whether the instruction is a two address instruction or not.
  558. Additionally the register set of the target machine has to be described.  Even
  559. the double register problem (e. g. IBM 370) can be handled.
  560.  
  561.      Two kinds of local register allocators can be requested: the on  the  fly
  562. register  allocator handles simple register sets.  However, it provides satis-
  563. fying results for many machines and is very efficient.  In some cases the gen-
  564. eral  register  allocator is necessary which performs some kind of look-ahead.
  565. Therefore it requires an extra pass.
  566.  
  567. 5.8.  Reuse
  568.  
  569.      Reuse is a library of reusable modules  oriented  towards  compiler  con-
  570. struction  [Gro87a].  It  contains  modules  or  abstract data types which are
  571. needed in almost every compiler:
  572.  
  573. -    a dynamic storage handler
  574.  
  575. -    a module that provides dynamic and flexible arrays
  576.  
  577. -    a facility to store strings of arbitrary length
  578.  
  579. -    a module for string handling
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                                                              8
  592.  
  593.  
  594. -    an identifier table which maps strings to unique integers using hashing
  595.  
  596. -    modules for commonly used data structures such as  sets  of  integers  or
  597.      binary relations between integers with no limitation of the domain
  598.  
  599. 6.  Application Experiences
  600.  
  601.      This section reports the experience of applying the tool box for  realis-
  602. tic problems.
  603.  
  604. 6.1.  Modula to C Translator
  605.  
  606.      The largest application for the tool box so far was the generation  of  a
  607. Modula-2  to  C translator [Mar90]. The program called Mtc translates Modula-2
  608. programs into readable C code without any restrictions (even nested procedures
  609. and modules).  It is largely generated and follows the compiler model proposed
  610. in section 4.  Instead of generating an intermediate language, Mtc produces  C
  611. code  and  therefore there is no need for a machine code generator.  It incor-
  612. porates as much of a semantic analysis as is needed for this task.  The seman-
  613. tic  analysis  is  rather complete and contains scope handling, name analysis,
  614. and type determination. However it does not check for semantic errors,  as  we
  615. assume that only correct programs will be translated.  Table 1 gives the sizes
  616. of the specifications and the generated source modules.  The design and imple-
  617. mentation of Mtc was completed within a master thesis and took approximately 6
  618. person months.  The program is stable and  has  already  converted  more  than
  619. 100,000 lines from Modula-2 to C.
  620.  
  621.      The binary program comprises 301,240 bytes.  It runs at a  speed  of  810
  622. tokens  per second or 167 lines per second on a SUN workstation (MC 68020 pro-
  623. cessor). This figures are computed by taking only the size of  the  translated
  624. modules  into account. If we include the definition modules which are imported
  625. transitively and which are scanned, parsed, and analyzed as well, we arrive at
  626. 1320  tokens  per second or 418 lines per second. In comparison, the compilers
  627.  
  628.  
  629. _________________________________________________________________________________
  630.  part                specification       source module             tool
  631. _________________________________________________________________________________
  632.                   formal  code  total  def.  impl.  total  name  references
  633. _________________________________________________________________________________
  634.  scanner            392    133   525     56   1320   1376  Rex   [Gro87b, Gro88]
  635.  parser             951     88  1039     81   3007   3088  Ell   [Gro88, GrV88]
  636.  tree               189     51   240    579   2992   3571  Ast   [Gro89c]
  637.  symbol table       115    938  1053    413   1475   1888  Ast   [Gro89c]
  638.  semantics         1886    151  2037      9   3288   3297  Ag    [Gro89d]
  639.  code generator    2793    969  3762     47   7309   7356  Estra [Vie89]
  640.  reusable parts     -      -      -     819   2722   3541  Reuse [Gro87a]
  641.  miscellaneous      -      -      -     698   3153   3851
  642. _________________________________________________________________________________
  643.  total             6326   2330  8656   2702  25266  27968
  644. _________________________________________________________________________________
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                 
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.                                      
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.                                                          
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.                                                                                 
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.         Table 1: Sizes of the specifications and source modules of Mtc
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.                                                                              9
  721.  
  722.  
  723. supplied by the manufacturer run at a speed of 97 lines per second  (excluding
  724. header  files) or 163 lines per second (including header files) in the case of
  725. C and 48 lines per second in the case of Modula-2. The run time of Mtc is dis-
  726. tributed as follows:
  727.  
  728.  
  729.                   scanning + parsing + tree construction   42 %
  730.                   semantic analysis                        33 %
  731.                   code generation                          25 %
  732.  
  733.  
  734. The semantic analysis spends 95 % in attribute computations  using  user  sup-
  735. plied  code  and  5 % in tree traversal or visit actions respectively.  By the
  736. way, there are up to five visits to 11 node types.
  737.  
  738.      Mtc uses approximately 360 K Bytes dynamic memory per 1000  source  lines
  739. to  store  the  abstract  syntax  tree,  the  attributes, and the symbol table
  740. without optimization of attribute storage. Another 600 K Bytes are used by the
  741. transformer  generated with Estra.  This is bearable with today's memory capa-
  742. cities.  Contrary to the literature this shows that it is  possible  to  store
  743. all  attributes  in  the  tree. We even do this for the environment attribute.
  744. This becomes possible by implementing the symbol table  as  an  abstract  data
  745. type in the target language. The implementation is time and space efficient by
  746. taking advantage of pointer semantics and structure sharing.
  747.  
  748. 6.2.  Code Generator for Modula-2 Compiler
  749.  
  750.      In another application we replaced the hand-written code generator of the
  751. GMD  Modula-2  compiler Mocka by two versions produced by Beg.  Target machine
  752. was the MC 68020 processor. The specification of the code generator  comprises
  753. 1625  lines.  It contains 187 rules and 99 intermediate language operators. To
  754. compare the quality of the generated code, we measured the execution time  for
  755. a collection of benchmark programs (see  Table  2).   Mocka  denotes  the  GMD
  756. Modula-2 compiler with the hand-written code generator, Begra and Begfly refer
  757. to the code generators produced by Beg with the general register allocator and
  758. the  on  the  fly register allocator respectively, and Sun is the SUN Modula-2
  759. compiler version 1.0. On the average code generators produced by Beg  generate
  760. code that is as fast as the one from the hand-written code generator.
  761.  
  762.  
  763.  
  764. _________________________________________________________________________________
  765.          Perm  Towers  Queens  Intmm  Mm   Puzzle  Quick  Tree  Bubble  FFT   R
  766. _________________________________________________________________________________
  767.  Mocka    40     58      37     53    103   285     32     72     56    152  888
  768.  Begra    42     57      35     54    104   297     32     58     56    153  888
  769.  Begfly   42     57      34     54    102   299     33     56     56    151  884
  770.  Sun      67     90      28     83    101   263     41     81     63    150  967
  771. _________________________________________________________________________________
  772.  
  773.  
  774.  
  775.  
  776.  
  777.        
  778.  
  779.  
  780.  
  781.  
  782.              
  783.  
  784.  
  785.  
  786.  
  787.                      
  788.  
  789.  
  790.  
  791.  
  792.                              
  793.  
  794.  
  795.  
  796.  
  797.                                     
  798.  
  799.  
  800.  
  801.  
  802.                                          
  803.  
  804.  
  805.  
  806.  
  807.                                                  
  808.  
  809.  
  810.  
  811.  
  812.                                                         
  813.  
  814.  
  815.  
  816.  
  817.                                                               
  818.  
  819.  
  820.  
  821.  
  822.                                                                       
  823.  
  824.  
  825.  
  826.  
  827.                                                                            
  828.  
  829.  
  830.  
  831.  
  832.                                                                                 
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.    Table 2: Comparison of code quality (run times in ticks = 1/60 seconds)
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.                                                                             10
  854.  
  855.  
  856.  
  857.  
  858.                                _______________
  859.                                 Mocka     2.9
  860.                                 Begfly    3.2
  861.                                 Begra     3.9
  862.                                 Sun      25.4
  863.                                _______________
  864.                               
  865.  
  866.  
  867.                                       
  868.  
  869.  
  870.                                              
  871.  
  872.  
  873.  
  874.  
  875.  
  876.            Table 3: Comparison of compilation times (times in sec.)
  877.  
  878.      Table 3 compares the run times of the compilers for processing a  program
  879. with 1116 lines. All measurements were carried out on a diskless SUN 3/60, all
  880. measured times are user times. The size of the code generator  increased  from
  881. 5140  lines (17,117 tokens) for the hand-written version to 9574 lines (27,044
  882. tokens).
  883.  
  884. 7.  Summary and Future Work
  885.  
  886.      We presented a complete tool box of  compiler  construction  tools  which
  887. supports  the  construction  of  all  phases of a compiler. The tools are very
  888. powerful and flexible and largely independent of each other.  They  support  a
  889. wide  range of compiler designs and allow the construction of production qual-
  890. ity compilers for many programming languages.   First  realistic  applications
  891. demonstrate the excellent performance of the tools.
  892.  
  893.      The optimization of attribute storage of Ag  will  be  improved  allowing
  894. attributes to be treated as global variables and global stacks.  The transfor-
  895. mation of non-OAG grammars into OAG ones should be automized.
  896.  
  897.      A redesign is planned for the tool  Estra.   The  specification  language
  898. will  become  simpler  and clearer and the tool will be integrated better with
  899. Ast.  The efficiency of the generated transformation modules can  be  improved
  900. both in terms of run time and storage consumption.
  901.  
  902.      The optimization phase of a compiler clearly needs to be supported.  This
  903. could  be  either  done  by  a reusable, language independent optimizer, by an
  904. optimizer which can be parameterized, or by some means of an optimizer genera-
  905. tor.
  906.  
  907.      The tool Beg will be extended in the directions of the  generation  of  a
  908. global  register allocator, support for instruction scheduling, and a facility
  909. for the direct generation of binary object code.
  910.  
  911. Acknowledgement
  912.  
  913.      We thank all that have contributed to the  development  of  this  toolbox
  914. either  by  active  participation or with their ideas: Michael Besser, Carsten
  915. Gerlhof, Bob Gray, Rudolf Landwehr, Matthias Martin,  Thomas  Mueller,  F.  W.
  916. Schroeer, Dirk Schwartz-Hertzner, Doris Vielsack, Bertram Vielsack und William
  917. M. Waite.
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.                                                                             11
  930.  
  931.  
  932. References
  933.  
  934. [ASU86]
  935.      A. V. Aho, R. Sethi and J. D. Ullman, Compilers: Principles,  Techniques,
  936.      and Tools, Addison Wesley, Reading, MA, 1986.
  937.  
  938. [DeP82]
  939.      F. DeRemer and T. Pennello, Efficient Computation of  LALR(1)  Look-Ahead
  940.      Sets, ACM Trans. Prog. Lang. and Systems 4, 4 (Oct. 1982), 615-649.
  941.  
  942. [Emm89a]
  943.      H. Emmelmann, Automatische Erzeugung  effizienter  Codegeneratoren,  GMD-
  944.      Studie Nr. 158, GMD Forschungsstelle an der Universitat Karlsruhe, 1989.
  945.  
  946. [Emm89b]
  947.      H. Emmelmann, BEG - A Back End Generator - User Manual, Arbeitspapier Nr.
  948.      420, GMD Forschungsstelle an der Universitat Karlsruhe, Dec. 1989.
  949.  
  950. [ESL89]
  951.      H. Emmelmann, F. W. Schroer and  R.  Landwehr,  BEG  -  a  Generator  for
  952.      Efficient Back Ends, SIGPLAN Notices 24, 7 (July 1989), 227-237.
  953.  
  954. [Gro87a]
  955.      J. Grosch, Reusable Software - A Collection of  Modula-Modules,  Compiler
  956.      Generation   Report  No.  4,  GMD  Forschungsstelle  an  der  Universitat
  957.      Karlsruhe, Sep. 1987.
  958.  
  959. [Gro87b]
  960.      J. Grosch, Rex - A Scanner Generator, Compiler Generation Report  No.  5,
  961.      GMD Forschungsstelle an der Universitat Karlsruhe, Dec. 1987.
  962.  
  963. [Gro88]
  964.      J. Grosch, Generators for High-Speed Front-Ends, LNCS 371,  (Oct.  1988),
  965.      81-92, Springer Verlag.
  966.  
  967. [GrV88]
  968.      J. Grosch and B. Vielsack, The Parser Generators Lalr and  Ell,  Compiler
  969.      Generation   Report  No.  8,  GMD  Forschungsstelle  an  der  Universitat
  970.      Karlsruhe, Apr. 1988.
  971.  
  972. [Gro89a]
  973.      J. Grosch, Efficient Generation of Lexical Analysers, Software-Practice &
  974.      Experience 19, 11 (Nov. 1989), 1089-1103.
  975.  
  976. [Gro89b]
  977.      J. Grosch, Efficient and Comfortable Error Recovery in Recursive  Descent
  978.      Parsers,  Compiler  Generation Report No. 19, GMD Forschungsstelle an der
  979.      Universitat Karlsruhe, Dec. 1989.
  980.  
  981. [Gro89c]
  982.      J. Grosch, Ast - A Generator for Abstract Syntax Trees (Revised Version),
  983.      Compiler   Generation   Report   No.  15,  GMD  Forschungsstelle  an  der
  984.      Universitat Karlsruhe, Aug. 1989.
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.                                                                             12
  996.  
  997.  
  998. [Gro89d]
  999.      J. Grosch, Ag - An Attribute  Evaluator  Generator,  Compiler  Generation
  1000.      Report  No.  16,  GMD Forschungsstelle an der Universitat Karlsruhe, Aug.
  1001.      1989.
  1002.  
  1003. [Gro90]
  1004.      J. Grosch, Object-Oriented Attribute  Grammars,  in  Proceedings  of  the
  1005.      Fifth International Symposium on Computer and Information Sciences (ISCIS
  1006.      V), A. E. Harmanci and E. Gelenbe (ed.),  Cappadocia,  Nevsehir,  Turkey,
  1007.      Oct. 1990, 807-816.
  1008.  
  1009. [Gro91a]
  1010.      J. Grosch,  Ast  -  A  Generator  for  Abstract  Syntax  Trees,  Compiler
  1011.      Generation  Report  No.  15,  GMD  Forschungsstelle  an  der  Universitat
  1012.      Karlsruhe, Sep. 1991.
  1013.  
  1014. [Gro91b]
  1015.      J. Grosch, Tool Support for Data Structures, Structured  Programming  12,
  1016.      (1991), 31-38.
  1017.  
  1018. [Joh75]
  1019.      S. C. Johnson, Yacc -  Yet Another  Compiler-Compiler,  Computer  Science
  1020.      Technical  Report  32, Bell Telephone Laboratories, Murray Hill, NJ, July
  1021.      1975.
  1022.  
  1023. [Kas80]
  1024.      U. Kastens, Ordered Attribute Grammars, Acta Inf. 13, 3 (1980), 229-256.
  1025.  
  1026. [Les75]
  1027.      M. E. Lesk,  LEX  -  A  Lexical  Analyzer  Generator,  Computing  Science
  1028.      Technical Report 39, Bell Telephone Laboratories, Murray Hill, NJ, 1975.
  1029.  
  1030. [Mar90]
  1031.      M. Martin, Entwurf und Implementierung  eines  Ubersetzers  von  Modula-2
  1032.      nach  C, Diplomarbeit, GMD Forschungsstelle an der Universitat Karlsruhe,
  1033.      Feb. 1990.
  1034.  
  1035. [Vie89]
  1036.      B.  Vielsack,  Spezifikation  und  Implementierung   der   Transformation
  1037.      attributierter   Baume,   Diplomarbeit,   GMD   Forschungsstelle  an  der
  1038.      Universitat Karlsruhe, June 1989.
  1039.  
  1040. [VSK89]
  1041.      H. H. Vogt, S. D. Swierstra and M.  F.  Kuiper,  Higher  Order  Attribute
  1042.      Grammars, SIGPLAN Notices 24, 7 (July 1989), 131-145.
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                                                                              1
  1061.  
  1062.  
  1063. Contents
  1064.  
  1065.         Abstract ........................................................    1
  1066. 1.      Introduction ....................................................    1
  1067. 2.      Design Goals ....................................................    2
  1068. 3.      Common Implementation Decisions .................................    2
  1069. 4.      Compiler Model ..................................................    3
  1070. 5.      The Tools .......................................................    4
  1071. 5.1.    Rex .............................................................    4
  1072. 5.2.    Lalr ............................................................    4
  1073. 5.3.    Ell .............................................................    5
  1074. 5.4.    Ast .............................................................    5
  1075. 5.5.    Ag ..............................................................    6
  1076. 5.6.    Estra ...........................................................    6
  1077. 5.7.    Beg .............................................................    7
  1078. 5.8.    Reuse ...........................................................    7
  1079. 6.      Application Experiences .........................................    8
  1080. 6.1.    Modula to C Translator ..........................................    8
  1081. 6.2.    Code Generator for Modula-2 Compiler ............................    9
  1082. 7.      Summary and Future Work .........................................   10
  1083.         Acknowledgement .................................................   10
  1084.         References ......................................................   11
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.